package com.zzhou.linkinfo.clock.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import com.zzhou.linkinfo.clock.Consts;
import com.zzhou.linkinfo.clock.R;
import com.zzhou.linkinfo.clock.entity.ClockFrequency;

import java.util.ArrayList;

/**
 * 打开频次adapter
 * Created by Carlos on 2019/03/18.
 */
public class ClockFrequencyAdapter extends BaseAdapter {
    private ArrayList<ClockFrequency> mData;
    private Context mContext;

    public ClockFrequencyAdapter(Context context, ArrayList<ClockFrequency> frequencies) {
        super(context);
        mContext = context;
        mData = frequencies;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.clock_item_clock_frequency, parent, false);
        final ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public class ViewHolder extends BaseViewHolder {
        View spaceView;
        TextView frequencyName;
        CheckBox frequencyCb;

        public ViewHolder(View itemView) {
            super(itemView);

            spaceView = itemView.findViewById(R.id.space_view);
            frequencyName = itemView.findViewById(R.id.frequency_name);
            frequencyCb = itemView.findViewById(R.id.frequency_checkbox);
        }

        @Override
        public void setData(int position) {
            final ClockFrequency frequency = mData.get(position);

            switch (frequency.getFrequencyType()) {
                case Consts.IClockFrequencyType.TYPE_EVERY_DAY:
                    initData(false, true);
                    break;
                case Consts.IClockFrequencyType.TYPE_NEXT_DAY:
                    initData(false, true);
                    break;
                case Consts.IClockFrequencyType.TYPE_CUSTOM_TITLE:
                    initData(false, false);
                    break;
                case Consts.IClockFrequencyType.TYPE_CUSTOM_MON:
                case Consts.IClockFrequencyType.TYPE_CUSTOM_TUES:
                case Consts.IClockFrequencyType.TYPE_CUSTOM_WED:
                case Consts.IClockFrequencyType.TYPE_CUSTOM_THUR:
                case Consts.IClockFrequencyType.TYPE_CUSTOM_FRI:
                case Consts.IClockFrequencyType.TYPE_CUSTOM_SAT:
                case Consts.IClockFrequencyType.TYPE_CUSTOM_SUN:
                    initData(true, true);
                    break;

            }
            frequencyName.setText(frequency.getName());
            frequencyCb.setChecked(frequency.isSelected() ? true : false);
            frequencyCb.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    updateFrequencyData(frequency);
                }
            });

        }

        private void initData(boolean hasSpace, boolean canSelected) {
            spaceView.setVisibility(hasSpace ? View.VISIBLE : View.GONE);
            frequencyCb.setVisibility(canSelected ? View.VISIBLE : View.GONE);
        }

        /**
         * 处理选择频次逻辑
         * 选择【每天】，则选择每周一、二、三、四、五、六、日；
         * 选择【隔天】，则选中每周一、三、五、日；
         * 若用户自由选中非“每天”或“隔天”则取消选择按钮【每天】或【隔天】选中状态
         */
        private void updateFrequencyData(ClockFrequency frequency) {
            for (ClockFrequency data : mData) {
                if (frequency.getFrequencyType() == Consts.IClockFrequencyType.TYPE_EVERY_DAY) {
                    if (data.getFrequencyType() == Consts.IClockFrequencyType.TYPE_NEXT_DAY ||
                            data.getFrequencyType() == Consts.IClockFrequencyType.TYPE_CUSTOM_TITLE) {
                        data.setSelected(false);
                    } else {
                        data.setSelected(true);
                    }
                } else if (frequency.getFrequencyType() == Consts.IClockFrequencyType.TYPE_NEXT_DAY) {
                    if (data.getFrequencyType() == Consts.IClockFrequencyType.TYPE_NEXT_DAY ||
                            data.getFrequencyType() == Consts.IClockFrequencyType.TYPE_CUSTOM_MON ||
                            data.getFrequencyType() == Consts.IClockFrequencyType.TYPE_CUSTOM_WED ||
                            data.getFrequencyType() == Consts.IClockFrequencyType.TYPE_CUSTOM_FRI ||
                            data.getFrequencyType() == Consts.IClockFrequencyType.TYPE_CUSTOM_SUN) {
                        data.setSelected(true);
                    } else {
                        data.setSelected(false);
                    }
                } else {
                    frequency.setSelected(frequencyCb.isChecked());

                    boolean isEveryDay = true;
                    boolean isNextDay = false;
                    for (int i = 3; i < mData.size(); i++) {
                        if (!mData.get(i).isSelected()) {
                            isEveryDay = false;
                            break;
                        }
                    }
                    if (!isEveryDay) {
                        if (mData.get(3).isSelected() && mData.get(5).isSelected() &&
                                mData.get(7).isSelected() && mData.get(9).isSelected() &&
                                !mData.get(4).isSelected() && !mData.get(6).isSelected() && !mData.get(8).isSelected()) {
                            isNextDay = true;
                        }
                    }
                    mData.get(0).setSelected(isEveryDay ? true : false);
                    mData.get(1).setSelected(isNextDay ? true : false);
                }
            }
            notifyDataSetChanged();
        }
    }

    /**
     * 获取最终选择后的频次列表数据
     *
     * @return
     */
    public ArrayList<ClockFrequency> getFinalFrequencyData() {
        return mData;
    }
}
